package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Attachment;
import com.ruoyi.system.domain.CourseAttachment;
import com.ruoyi.system.mapper.AttachmentMapper;
import com.ruoyi.system.mapper.AttachmentTypeMapper;
import com.ruoyi.system.mapper.CourseAttachmentMapper;
import com.ruoyi.system.service.AttachmentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 附件管理表(Attachment)表服务实现类
 */
@Service("attachmentService")
public class AttachmentServiceImpl implements AttachmentService {
    @Resource
    private AttachmentMapper attachmentMapper;

    @Resource
    private CourseAttachmentMapper courseAttachmentMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param attachmentId 主键
     * @return 实例对象
     */
    @Override
    public Attachment queryById(Long attachmentId) {
        return this.attachmentMapper.queryById(attachmentId);
    }

    @Override
    public List<Attachment> list(Attachment attachment) {
        return attachmentMapper.list(attachment);
    }

//    /**
//     * 分页查询
//     *
//     * @param attachment 筛选条件
//     * @return 查询结果
//     */
//    @Override
//    public List<Attachment> queryList(Attachment attachment) {
//        return attachmentMapper.queryAllByLimit(attachment);
//    }




    /**
     * 新增数据
     *
     * @param attachment 实例对象
     * @return 实例对象
     */
    @Override
    public Attachment insert(Attachment attachment) {
        this.attachmentMapper.insert(attachment);
        return attachment;
    }

    /**
     * 修改数据
     *
     * @param attachment 实例对象
     * @return 实例对象
     */
    @Override
    public Attachment update(Attachment attachment) {
        this.attachmentMapper.update(attachment);
        return this.queryById(attachment.getAttachmentId());
    }

    /**
     * 通过主键删除数据
     *
     * @param attachmentId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long attachmentId) {
        return this.attachmentMapper.deleteById(attachmentId) > 0;
    }


    @Override
    public List<Attachment> getAttachmentByCourseIdAndType(String courseId, String attachmentType) {
        List<CourseAttachment> courseAttachments = courseAttachmentMapper.queryAllByCourseIdAndAttachmentType(courseId, attachmentType);
        List<Attachment> attachments = new ArrayList<>();
        for (CourseAttachment courseAttachment : courseAttachments) {
            attachments.add(attachmentMapper.queryById(courseAttachment.getAttachmentId()));
        }
        return attachments;
    }

}
